$(function(){
	$('.datepicker').datepicker({
	     format: "yyyy-mm-dd",
	     language: "zh-CN",
	     orientation: "top left",
	     autoclose:true
	});
	var  dataTable = $('#supplierInfo').dataTable({
//		"aoColumnDefs" : [ {
//	        sDefaultContent : '',
//	        aTargets : [ '_all' ]
//	    } ],
		"bPaginate": false,
		bFilter: false,
//        ajax: 'getAllSuppliers?pageNum=1&pageSize=10',
        aoColumns: [
            {mData:"supplierCode"},
            {mData:"supplierName"},
            {mData:"skuNum"},
            {mData:"totalNum"},
            {mData:"receivedNum"},
            {mData:"unreceivedNum"},
            {mData:"supplierId"}  
        ],
        columnDefs:[{
        	targets:[6],
        	data:"supplierId",
        	render: function(data, type, full) {
        		if($(".action").val().indexOf("退") == -1){
        			 return "<a href='/po_purchase/purchaseOrderDetail?supplierId="+ data+"&supplierName="+full["supplierName"].replace(/\s+/g,"")+ "'>"+$(".action").val()+"</a>";
        		}
        		 return "<a href='/purchaseReturnOrder/purchaseReturnOrderDetail?supplierId="+ data+"&supplierName="+full["supplierName"].replace(/\s+/g,"")+ "'>"+$(".action").val()+"</a>";
        	}
        }]
    } );
	
	RefreshTableByData('#supplierInfo','getAllSuppliers');
	$('.reset').click(function(){
		console.log("tes");
		$("#purchaseName").val("");
		$("#purchaseNo").val("");
	});
	
//	var oSettings = dataTable.fnSettings();
	$(".searchBtn").click(function(){
		var keywords, url;
		if($("#purchaseNo").val().trim() == "" && $("#purchaseName").val().trim() == ""){
			alert("查询内容不能为空");
			return false;
		}
		else if($("#purchaseNo").val().trim() !== ""){
			keywords = $("#purchaseNo").val();
			url = 'supplierCode?pageNum=1&pageSize=10&supplierCode='+keywords;
		}else{
			keywords = $("#purchaseName").val();
			url = 'supplierName?pageNum=1&pageSize=10&supplierName='+keywords;
		}
		
//		oSettings.sAjaxSource = url;
//		dataTable._fnAjaxUpdate();
		RefreshTable("#supplierInfo",url);
//		dataTable.fnDraw();
	});
	
	function RefreshTableByData(tableId, urlData)
    {
      //Retrieve the new data with $.getJSON. You could use it ajax too
      $.getJSON(urlData, null, function( json )
      {
    	  console.log(json);
        table = $(tableId).dataTable();
        oSettings = table.fnSettings();

        table.fnClearTable(this);

        for (var i=0; i<json.length; i++)
        {
          table.oApi._fnAddData(oSettings, json[i]);
        }

        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        table.fnDraw();
      });
    }
	function RefreshTable(tableId, urlData)
    {
      //Retrieve the new data with $.getJSON. You could use it ajax too
      $.getJSON(urlData, null, function( json )
      {
    	  console.log(json);
        table = $(tableId).dataTable();
        oSettings = table.fnSettings();

        table.fnClearTable(this);

        for (var i=0; i<json.data.length; i++)
        {
          table.oApi._fnAddData(oSettings, json.data[i]);
        }

        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        table.fnDraw();
      });
    }
});